<?php
namespace app\backend\controller;

use app\backend\model\People as PeopleModel;
use app\backend\model\Department as DepartmentModel;
use app\backend\model\People_intro;
use app\backend\model\Team as TeamModel;
use app\backend\model\Menu as MenuModel;
use app\backend\model\Duty as DutyModel;
use app\backend\model\People_intro as People_introModel;
use think\Db;
use think\Request;
use app\backend\org\Helper;
use app\backend\org\Image;

class People extends Base
{

    // 列表展示
    public function index($team_id=1)
    {
        $team_id = Request::instance()->param("team_id");
        $model = new PeopleModel;
        // 获取分页显示
        $result = $model->getAll($team_id)->paginate(5);

        $page = $result->render();
        $depart = Db::query("select department_name_cn,department_id from ls_department where delete_time is NULL ");
        //var_dump($depart);exit;
        if($team_id == 1){
            foreach($result as $a=>$b){
                foreach($depart as $k=>$v){
                    if($b["department_id"] == $v["department_id"] ){
                        $result[$a]["depart_name"] = $v["department_name_cn"];
                    }
                }
            }
        }else{
            foreach($result as $a=>$b){
                $result[$a]["depart_name"] = "无";
            }

        }

        //team_id=1是不显示职称
        $this->assign('page',$page);
        $this->assign('people',$result);
        $this->assign('empty',"暂无数据");
        //$this->assign('jsController','people');
        $this->assign('jsId',$team_id);
        return $this->fetch();

    }

    //新建新建人员，select出部门下拉框
    public function people_create(){
        if (Request::instance()->isPost())
        {
            $model = new PeopleModel();
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            $post["team_id"] = $post["nav-select"];
            $post["department_id"] = $post["depart-select"];
            unset($post["nav-select"]);
            unset($post["depart-select"]);
            //var_dump($post);exit;
            // die();
            //调用图片上传助手类
            if(!(Image::image($post,PAGE_PATH))){
                $this->error("图片上传失败");
            }
            //var_dump($post);exit;
            unset($post["img_type"]);
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select people_id,name_cn from ls_people where `name_cn`='".$post['name_cn']."'and `team_id`='".$post['team_id']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('people_update',array('id'=>$name[0]['people_id'])));
            }
            //手动排序
            $maual_sort = Db::query("select sort from ls_people where team_id={$post["team_id"]} and delete_time is null");
            //substr($maual_sort)
            //var_dump($maual_sort);exit;
            //sort函数 排序 [0]最小值
            if(!empty($maual_sort)){
                sort($maual_sort);
                //var_dump($v);exit;
                //最xiao值
                $max = $maual_sort[0];
                $str= $max["sort"]-1;
                $post["sort"] = $str.","."99,99";
            }else{
                $post["sort"] = "99,99,99";
            }

            //var_dump( $post["sort"]);exit;
            // 验证表单并插入数据库
            $result = $this->validate($post,'People.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            //$team_id = Request::instance()->param("team_id");
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('people/index',["team_id"=>$post["team_id"]]);
            }
        }
//前端页面分配select两个变量
        $modelD = new DepartmentModel;
        $modelT = new TeamModel;
        $depart = $modelD->getAll()->select();
        $team = $modelT->getAll()->select();
        $this->assign('depart',$depart);
        $this->assign('team',$team);

        return $this->fetch();
    }

    //人员修改
    public function people_update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new PeopleModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
//var_dump($post);exit;
            //调用图片上传助手类
            if(!(Image::image($post,PAGE_PATH))){
                $this->error("图片上传失败");
            }
            //修改部门
            $post["department_id"] = $post["depart-select"];
            //var_dump($post);exit;
            unset($post["depart-select"]);
            //var_dump($post);exit;
            unset($post["img_type"]);
            //查询出数据库的内容 分配已知内容给前台

            //验证表单并插入数据库
            $result = $this->validate($post,'People.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['people_id'=>$id]);
            $team = Db::query("select team_id from ls_people where people_id=$id and delete_time is NULL ");
//index页面展示的部门名称

            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->success('修改成功',url('people/index',['team_id'=>$team[0]["team_id"]]));
            }
        }
        //分配页面控制变化
        $team = Db::query("select team_id from ls_people where people_id=$id and delete_time is NULL ");
        //var_dump($con);exit;
        if($team[0]['team_id']==1){
            $this->assign('team', 1);
        }elseif ($team[0]['team_id']==2){
            $this->assign('team', 2);
        }elseif ($team[0]['team_id']==3){
            $this->assign('team', 3);
        }
        $depart_old = Db::query("select department_id from ls_people where people_id=$id and delete_time is NULL ");

        //var_dump($depart_old);exit;
        //根据主键插记录
        $record = PeopleModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('depart_old',$depart_old[0]["department_id"]);

        $this->assign('people', $record);
        $this->assign('act','index');

        //前端页面分配select两个变量
        $modelD = new DepartmentModel;
       // $modelT = new TeamModel;
        $depart = $modelD->getAll()->select();
        //$team = $modelT->getAll()->select();
        $this->assign('depart',$depart);
        //$this->assign('team',$team);
        return $this->fetch();
    }

    //删除人员  软删除，记录删除时间
    public function people_delete($id)
    {
        $team_id = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("team_id");
        $duty_id = DutyModel::field(["duty_id"])->where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->select();
        $intro_id = People_introModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("intro_id");
   //var_dump($duty_id);exit;

        //删除职称
        foreach($duty_id as $k=>$v){
            DutyModel::destroy($v["duty_id"]);
        }
        //删除简介
        People_introModel::destroy($intro_id);
        //删除人员
        PeopleModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('people/index',["team_id"=>$team_id]);
        return $this->fetch();
       /* $team_id = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("team_id");
        PeopleModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('people/index',["team_id"=>$team_id]);
        return $this->fetch();*/
    }

    //启用人员
    public function people_enable($id){
        DepartmentModel::update(['is_enable'=>1],['department_id'=>$id]);
        $this->redirect('department/index');
        return $this->fetch();
    }

    //增改简介
    public function people_intro($id){
        if (Request::instance()->isPost())
        {
            //var_dump($intro_id);exit;
            $model = new People_introModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            $post["content_cn"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_cn"]);
            $post["content_en"]=preg_replace('/<p><br\/><\/p>/','<br/>',$post["content_en"]);
            //var_dump($post);exit;
            $post["people_id"] = $id;

            // 判断other表已有内容不能重复新增去修改
            $intro_id = People_introModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("intro_id");
            //var_dump($name);exit;
            if(!empty($intro_id)){
                $update_intro = Db::execute("update ls_people_intro set content_cn='{$post['content_cn']}',content_en='{$post['content_en']}' where intro_id = '{$intro_id}' and delete_time is null ");
                //return $this->redirect(url('modules_update',array('id'=>$id)));
                if(!$update_intro){
                    // 验证失败 输出错误信息
                    dump($update_intro);
                    return $this->error($msg = 'intro表修改失败');
                }
            }else{
                //插入intro数据表
                $intro_con = $this->validate($post,'PeopleIntro.edit');
                if(true !== $intro_con){
                    // 验证失败 输出错误信息
                    dump($intro_con);
                    return $this->error($msg = '参数错误，验证失败');
                }
                // 登录人的ID
                $post["creator_id"] = \think\Session::get('login_id');
                //$model->insert($post);
                // 过滤掉OTHER表中没有的字段，保存
                $model = new People_introModel;
                $line = $model->allowField(true)->save($post);
                if(!$line){
                    return $this->error($this->update_error);
                }
            }

            //验证表单并插入数据库
            $result = $this->validate($post,'PeopleIntro.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['intro_id'=>$intro_id]);
            $team_id = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("team_id");
            //var_dump($team_id);exit;
                if(false === $result){
                    $this->error($this->update_error);
                } else {
                    $this->success('修改成功',url('people/index',['team_id'=>$team_id]));
                }
        }
        $intro_id = People_introModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("intro_id");
        if(!empty($intro_id)){
            $record = People_introModel::get($intro_id);
            $this->assign('intro',$record);
        }else{
            $record["content_cn"] = "";
            $record["content_en"] = "";
            $this->assign('intro',$record);
        }
        $people_name = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("name_cn");
        $this->assign('name',$people_name);
        $id = Request::instance()->param("id");
        $this->assign('id',$id);
        $team_id = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("team_id");
        $this->assign('team_id',$team_id);

        return $this->fetch();
    }

    //删除简介
    public function people_intro_delete($id){
        $intro_id = People_introModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("intro_id");
        $team_id = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("team_id");
        if(!isset($intro_id)){
            return $this->error("没有数据");
        }
        People_introModel::destroy($intro_id);
        $this->redirect(url('people/index',["team_id"=>$team_id]));
    }

    //team列表
    public function team(){
        //$team_id = Request::instance()->param("team_id");
        $model = new TeamModel;
        // 获取分页显示
        $result = $model->getAll()->paginate(5);
        $page = $result->render();

        //team_id=1是不显示职称
        $this->assign('page',$page);
        $this->assign('team',$result);
        $this->assign('empty',"暂无数据");
        //$this->assign('jsController','people');
        //$this->assign('jsId',$team_id);
        return $this->fetch();
    }

    //team新建
    public function team_create(){
        if (Request::instance()->isPost())
        {
            $model = new TeamModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            //var_dump($post);exit;
            // die();
            // 判断已有内容不能重复新增去修改
            $name = Db::query("select * from ls_team where `team_name_cn`='".$post['team_name_cn']."' and delete_time is NULL ");
            //var_dump($name);exit;
            if(!empty($name)){
                return $this->redirect(url('team_update',array('id'=>$name[0]['team_id'])));
            }

            // 验证表单并插入数据库
            $result = $this->validate($post,'Team.add');
            if(true !== $result){
                // 验证失败 输出错误信息
                dump($result);
                return $this->error($msg = '参数错误，验证失败');
            }

            // 登录人的ID
            $post["creator_id"] = \think\Session::get('login_id');
            //$model->insert($post);

            // 跳转到显示页面
            // 插入数据库后修改数据库跳转连接
            $line = $model->save($post);
            //var_dump($model);
            //var_dump($line);die();
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('people/team');
            }
        }

        //tab_id 属于领星人之下
       $tab_id =  Db::name("tab")->where(["delete_time"=>['exp','is null'],"tab_name_cn"=>'领星人'])->value("tab_id");
        //var_dump($tab_id);exit;
        $this->assign('tab_id',$tab_id);
        return $this->fetch();
    }

    //team修改
    public function team_update($id)
    {
        if (Request::instance()->isPost())
        {
            $model = new TeamModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量

            //验证表单并插入数据库
            $result = $this->validate($post,'Team.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['team_id'=>$id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->redirect('people/team');
            }
        }
        //根据主键插记录
        $record = TeamModel::get($id);
        //var_dump($record);exit;
        $this->assign('id',$id);
        $this->assign('team', $record);
        $this->assign('act','index');
        return $this->fetch();
    }

    //删除team
    public function team_delete($id)
    {
        TeamModel::destroy($id);//软删除
        //AlltimeModel::destroy($id,true);//真实删除

        $this->redirect('people/team');
        return $this->fetch();
    }


}